$(document).ready(function () {
	// 新闻管理
	// 新闻删除功能
	$('button[data-value="newsDelete"]').click(function () {
		var newsId = $(this).closest('tr').find('td').eq(0).text();
		var $thisTr = $(this).closest('tr');
		var data = {
			newsId: newsId
		};
		$.post('/admin/deleteNews', data, function (result) {
			if(result.status == 1) {
				swal({
				  title: "删除成功",
				  type: "success"
				}, function () {
					$thisTr.remove();
				});
			}
		});
	});
	// 新闻修改功能
	$('button[data-value="newsUpdate"]').click(function () {
		var newsId = $(this).closest('tr').find('td').eq(0).text();
		var classify = $(this).closest('table').parent().prev('.page-header').find('h3').text();
		classify = classify.replace("新闻", "");
		var url = '/admin/publish?classify=';
		url += classify;
		url += '&newsId=';
		url += newsId;
		window.location.href = url;
	});
	
	
	// 用户管理
	// 用户删除功能
	$('button[data-value="userDelete"]').click(function () {
		var userId = $(this).closest('tr').find('td').eq(0).text();
		var $thisTr = $(this).closest('tr');
		var data = {
			userId: userId
		};
		$.post('/admin/deleteUser', data, function (result) {
			if(result.status == 1) {
				swal({
				  title: "删除成功",
				  type: "success"
				}, function () {
					$thisTr.remove();
				});
			}
		});
	});
	// 用户状态修改功能
	$('.user-admin').click(function (event) {
		var $thisTarget = $(event.target);
		
		if($thisTarget.is('a[data-value="changeState"]')) {
			event.preventDefault();
			
			var state = $thisTarget.text();
			var data = {
				state: state,
				userId: $thisTarget.closest('tr').find('td').eq(0).text()
			}
			
			$.post('/admin/changeState', data, function (result) {
				if(result.status == 1) {
					swal({
					  title: "修改成功",
					  type: "success"
					}, function () {
						$thisTarget.closest('tr').find('td').eq(5).text(result.data.state);
					});
				}
			});
		}
	});
	
	
	// 管理员管理	
	// 增加管理员功能
	$('button[data-value="createAdmin"]').click(function () {
		var $thisBody = $(this).parent().parent().find('tbody');
		$('.modal-content').load('./register', function () {
			
				// 验证事件
		  		$('#registerForm input').blur(function () {
		  			// 昵称
		  			if($(this).attr('name') == "name" ){
		  				$(this).siblings('p.text-danger').remove();
		  				if($(this).val() == ""){
		  					$(this).parent().append('<p class="text-danger">昵称不能为空.</p>');
		  				}
		  			}else if($(this).attr('name') == "account") {
		  				$(this).siblings('p.text-danger').remove();
		  				if($(this).val().length < 6){
		  					$(this).parent().append('<p class="text-danger">账号最少请输入7位数.</p>');
		  				}
		  			}else if($(this).attr('name') == "password") {
		  				$(this).siblings('p.text-danger').remove();
		  				if($(this).val() == ""){
		  					$(this).parent().append('<p class="text-danger">密码不能为空.</p>');
		  				}
		  			}else if($(this).attr('id') == "rePassword") {
		  				$(this).siblings('p.text-danger').remove();
		  				if($(this).val() != $('#password').val()){
		  					$(this).parent().append('<p class="text-danger">两次密码不一样.</p>');
		  				}
		  			}else if($(this).attr('name') == "email" ) {
		  				$(this).siblings('p.text-danger').remove();
		  				if($(this).val() == ""){
		  					$(this).parent().append('<p class="text-danger">邮箱不能为空.</p>');
		  				}
		  			}
		  		});
				
				$('#registerForm').submit(function (event) {
					event.preventDefault();
					
					var flag = 0;
					$('#registerForm input').each(function (index, item) {
						if($(this).val() == "") {
							$(this).siblings('p.text-danger').remove();
							var text = $(this).closest('div.form-group').find('label').text();
							$(this).parent().append('<p class="text-danger">' + text + '不能为空.</p>');
							flag = 1;
						}else if($(this).parent().find('p.text-danger').length > 0) {
							flag = 1;
						}
					});
					if(flag) {
						return false;
					}
					
					var formValues = $(this).serialize();
					$.post('/admin/createAdmin', formValues, function (result) {
						if(result.status == 1) {
							swal({
							  title: "创建成功",
							  type: "success"
							}, function () {
								$('#modal').modal('hide');
								var html = "";
								
								html +=	'<tr><td>';
						    	html +=	result.data.id;
						    	html +=	'</td><td>';
						    	html +=	result.data.name;
						    	html +=	'</td><td>';
						    	html +=	result.data.account;
						    	html +=	'</td><td>';
						    	html +=	result.data.email;
						    	html +=	'</td><td>';
						    	html +=	result.data.createTime;
						    	html +=	'</td><td>';
						    	html +=	result.data.state;
						    	html +=	'</td><td>';
						    	html +=	result.data.power;
						    	html +=	'<td> <div class="btn-group"> <button type="button" class="btn btn-info btn-sm dropdown-toggle changeStatus" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> 状态修改 <span class="caret"></span> </button> <ul class="dropdown-menu"> <li><a href="#" data-value="changeState">正常</a></li> <li><a href="#" data-value="changeState">禁言</a></li> <li><a href="#" data-value="changeState">冻结</a></li> </ul></div> <div class="btn-group"> <button type="button" class="btn btn-info btn-sm dropdown-toggle addPower" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> 增加权限 <span class="caret addPower"></span> </button> <ul class="dropdown-menu"> <li><a href="#" data-power="2" data-value="addPower">滚动</a></li> <li><a href="#" data-power="4" data-value="addPower">社会</a></li> <li><a href="#" data-power="8" data-value="addPower">娱乐</a></li> <li><a href="#" data-power="16" data-value="addPower">科技</a></li> <li><a href="#" data-power="32" data-value="addPower">体育</a></li> <li><a href="#" data-power="64" data-value="addPower">财经</a></li> <li><a href="#" data-power="1" data-value="addPower">超级管理员</a></li> </ul> </div> <div class="btn-group"> <button type="button" class="btn btn-info btn-sm dropdown-toggle cutPower" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> 删除权限 <span class="caret cutPower"></span> </button> <ul class="dropdown-menu"> <li><a href="#" data-power="2" data-value="cutPower">滚动</a></li> <li><a href="#" data-power="4" data-value="cutPower">社会</a></li> <li><a href="#" data-power="8" data-value="cutPower">娱乐</a></li> <li><a href="#" data-power="16" data-value="cutPower">科技</a></li> <li><a href="#" data-power="32" data-value="cutPower">体育</a></li> <li><a href="#" data-power="64" data-value="cutPower">财经</a></li> <li><a href="#" data-power="1" data-value="cutPower">超级管理员</a></li> </ul> </div> <button class="btn btn-sm btn-danger" data-value="userAdminDelete">删除</button> </td>';
	 
								$thisBody.prepend(html);
							});
						}
					});
				});
				
		});
		$('#modal').modal();
	});
	// 管理员权限功能
	$('.manager-admin').click(function (event) {
		var $thisTarget = $(event.target);

		// 修改权限
		if($thisTarget.is('a[data-value="changeState"]')) {
			event.preventDefault();
			
			var state = $thisTarget.text();
			var data = {
				state: state,
				userId: $thisTarget.closest('tr').find('td').eq(0).text()
			}
			
			$.post('/admin/changeState', data, function (result) {
				if(result.status == 1) {
					swal({
					  title: "修改成功",
					  type: "success"
					}, function () {
						$thisTarget.closest('tr').find('td').eq(5).text(result.data.state);
					});
				}
			});
		}
		// 管理员删除功能
		if($thisTarget.is('button[data-value="userAdminDelete"]')) {
			var userId = $thisTarget.closest('tr').find('td').eq(0).text();
			var $thisTr = $thisTarget.closest('tr');
			var data = {
				userId: userId
			};
			$.post('/admin/deleteUserAdmin', data, function (result) {
				if(result.status == 1) {
					swal({
					  title: "删除成功",
					  type: "success"
					}, function () {
						$thisTr.remove();
					});
				}
			});
		// 控制增加权限按钮	
		}else if($thisTarget.is('.addPower')) {
			var arr = [];
			var powerStr = $thisTarget.closest('tr').find('td').eq(6).text();
			arr = powerStr.split(',');
			for(var i=0; i<arr.length; i++) {
				$thisTarget.closest('.btn-group').find('a').each(function (index, item) {
					if($(this).text() == arr[i]) {
						$(this).remove();
					}
				});
			}
		// 控制删除权限按钮
		}else if($thisTarget.is('.cutPower')) {
			var arr = [];
			var powerStr = $thisTarget.closest('tr').find('td').eq(6).text();
			arr = powerStr.split(',');
			$thisTarget.closest('.btn-group').find('a').each(function (index, item) {
				var flag = 1;
				for(var i=0; i<arr.length; i++) {
					if($(item).text() == arr[i]) {
						flag = 0;
					}
				}
				if(flag) {
					$(item).remove();
				}
			});
		// 增加权限
		}else if($thisTarget.is('a[data-value="addPower"]')) {
			event.preventDefault();
		
			var $this = $thisTarget;
			var data = {
				userId: $this.closest('tr').find('td').eq(0).text(),
				power: parseInt($this.attr('data-power'))
			};
			
			$.post('/admin/addPower', data, function (result) {
				if(result.status == 1) {
					swal({
					  title: "增加权限修改成功",
					  type: "success"
					}, function () {
						$this.closest('tr').find('td').eq(6).text(result.data.power);
					});
				}
			});
		// 删除权限
		}else if($thisTarget.is('a[data-value="cutPower"]')) {
			event.preventDefault();
		
			var $this = $thisTarget;
			var data = {
				userId: $this.closest('tr').find('td').eq(0).text(),
				power:  parseInt($this.attr('data-power'))
			};
			
			$.post('/admin/cutPower', data, function (result) {
				if(result.status == 1) {
					swal({
					  title: "删除权限修改成功",
					  type: "success"
					}, function () {
						$this.closest('tr').find('td').eq(6).text(result.data.power);
					});
				}
			});
		}
	});
});
